cmake_minimum_required(VERSION 3.10)
project(HttpServer VERSION 1.0.0 LANGUAGES CXX)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 设置编译选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -g")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")

# 查找依赖包
find_package(OpenSSL REQUIRED)
find_package(Threads REQUIRED)

# 查找muduo库
find_library(MUDUO_NET_LIB muduo_net REQUIRED)
find_library(MUDUO_BASE_LIB muduo_base REQUIRED)

# 查找MySQL库（可选）
find_library(MYSQL_CPPCONN_LIB mysqlcppconn)
find_library(MYSQL_CLIENT_LIB mysqlclient)

# 设置头文件包含目录
set(INCLUDE_DIRS
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/include/http
    ${PROJECT_SOURCE_DIR}/include/router
    ${PROJECT_SOURCE_DIR}/include/session
    ${PROJECT_SOURCE_DIR}/include/middleWare
    ${PROJECT_SOURCE_DIR}/include/middleWare/CORS
    ${PROJECT_SOURCE_DIR}/include/utils
    ${PROJECT_SOURCE_DIR}/include/utils/DbPool
)

# 如果找到MySQL头文件目录，添加到包含路径
if(EXISTS "/usr/include/mysql-cppconn-8")
    list(APPEND INCLUDE_DIRS "/usr/include/mysql-cppconn-8")
endif()
if(EXISTS "/usr/include/mysql")
    list(APPEND INCLUDE_DIRS "/usr/include/mysql")
endif()

# 收集源文件 - 使用更精确的文件收集方式
file(GLOB HTTP_SOURCES
    "${PROJECT_SOURCE_DIR}/src/http/*.cpp"
)

file(GLOB ROUTER_SOURCES
    "${PROJECT_SOURCE_DIR}/src/router/*.cpp"
)

file(GLOB SESSION_SOURCES
    "${PROJECT_SOURCE_DIR}/src/session/*.cpp"
)

file(GLOB MIDDLEWARE_SOURCES
    "${PROJECT_SOURCE_DIR}/src/middleWare/*.cpp"
)

file(GLOB CORS_SOURCES
    "${PROJECT_SOURCE_DIR}/src/middleWare/CORS/*.cpp"
)

file(GLOB UTILS_SOURCES
    "${PROJECT_SOURCE_DIR}/src/utils/DbPool/*.cpp"
)

# 合并所有源文件
set(ALL_SOURCES
    ${HTTP_SOURCES}
    ${ROUTER_SOURCES}
    ${SESSION_SOURCES}
    ${MIDDLEWARE_SOURCES}
    ${CORS_SOURCES}
    ${UTILS_SOURCES}
)

# 创建静态库
add_library(${PROJECT_NAME} STATIC ${ALL_SOURCES})

# 设置库的包含目录
target_include_directories(${PROJECT_NAME}
    PUBLIC
        $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
    PRIVATE
        ${INCLUDE_DIRS}
        ${OPENSSL_INCLUDE_DIR}
)

# 设置库的链接依赖
target_link_libraries(${PROJECT_NAME}
    PUBLIC
        ${MUDUO_NET_LIB}
        ${MUDUO_BASE_LIB}
        Threads::Threads
        OpenSSL::SSL
        OpenSSL::Crypto
)

# 如果找到MySQL库，添加链接
if(MYSQL_CPPCONN_LIB)
    target_link_libraries(${PROJECT_NAME} PUBLIC ${MYSQL_CPPCONN_LIB})
    target_compile_definitions(${PROJECT_NAME} PUBLIC ENABLE_MYSQL_SUPPORT)
endif()
if(MYSQL_CLIENT_LIB)
    target_link_libraries(${PROJECT_NAME} PUBLIC ${MYSQL_CLIENT_LIB})
endif()

# 设置库的属性
set_target_properties(${PROJECT_NAME} PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION ${PROJECT_VERSION_MAJOR}
    POSITION_INDEPENDENT_CODE ON
)

# 添加编译定义
target_compile_definitions(${PROJECT_NAME}
    PRIVATE
        $<$<CONFIG:Debug>:DEBUG>
        $<$<CONFIG:Release>:NDEBUG>
)

# 可选：创建测试目标
if(EXISTS "${PROJECT_SOURCE_DIR}/test")
    option(BUILD_TESTS "Build test programs" OFF)
    if(BUILD_TESTS)
        enable_testing()
        add_subdirectory(test)
    endif()
endif()

# 安装配置
install(TARGETS ${PROJECT_NAME}
    EXPORT HttpServerTargets
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    INCLUDES DESTINATION include
)

install(DIRECTORY include/
    DESTINATION include
    FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
)

# 打印配置信息
message(STATUS "=== HttpServer Library Configuration ===")
message(STATUS "Project: ${PROJECT_NAME} v${PROJECT_VERSION}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "Source files found: ${ALL_SOURCES}")
message(STATUS "OpenSSL found: ${OPENSSL_FOUND}")
message(STATUS "OpenSSL version: ${OPENSSL_VERSION}")
message(STATUS "Muduo libraries: ${MUDUO_NET_LIB}, ${MUDUO_BASE_LIB}")
if(MYSQL_CPPCONN_LIB)
    message(STATUS "MySQL C++ Connector: ${MYSQL_CPPCONN_LIB}")
else()
    message(STATUS "MySQL C++ Connector: NOT FOUND (optional)")
endif()
if(MYSQL_CLIENT_LIB)
    message(STATUS "MySQL Client: ${MYSQL_CLIENT_LIB}")
else()
    message(STATUS "MySQL Client: NOT FOUND (optional)")
endif()
message(STATUS "=========================================")